package me.zhengjie.modules.forecast.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.heshuo.amm.exception.DatabaseException;
import com.heshuo.amm.model.METStation;
import com.heshuo.amm.model.PLMessage;
import com.heshuo.amm.model.SystemConfig;
import com.heshuo.amm.model.Takeoff;
import com.heshuo.amm.model.TakeoffItem;
import com.heshuo.amm.model.User;
import com.heshuo.amm.service.IMETAuthorityService;
import com.heshuo.amm.service.ISystemConfigService;
import com.heshuo.amm.service.ITakeoffService;

import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserDTO;
import me.zhengjie.utils.SecurityUtils;

/**
 * @author maxd
 * @date 2019-10-29
 */
@RestController
@RequestMapping("api")
public class TakeOffController {

	@Autowired
	com.heshuo.amm.service.IFlightConfigService flightConfigService;
	@Autowired
	ISystemConfigService systemConfigService;
	@Autowired
	ITakeoffService takeoffService;

    @Autowired
    private UserService userService;

	@Autowired
	IMETAuthorityService mETAuthorityService; 
	
    @GetMapping(value = "/takeoff/getConfig")
    public ResponseEntity getConfig(){
    	SystemConfig takeoffSysCfg = systemConfigService.getSystemConfig(User.USER_ADMIN_ID,SystemConfig.TAKEOFF);
        return new ResponseEntity(takeoffSysCfg, HttpStatus.OK);
    }
    @PostMapping(value = "/takeoff/save")
    public ResponseEntity save(@RequestBody SystemConfig vmsg){
    	systemConfigService.saveSystemConfig(vmsg);
        return new ResponseEntity(true, HttpStatus.OK);
    }
    @GetMapping(value="/takeoff/getLastMessage")
    public ResponseEntity getLastMessage(@RequestParam(value="airport",required=false) String airport){
    	Takeoff to = null;
		try {
			UserDTO userDto = userService.findById(SecurityUtils.getUserId());
			String code = userDto.getDept().getAirportCode();
			if(StringUtils.isBlank(code)){
				code=airport;
			}
			METStation ms = mETAuthorityService.getMETStationBycode(code);
			to = takeoffService.getLatestTakeoff(ms,new Date());
			if(to.getItems()==null||to.getItems().size()==0){
				to=null;
			}else{
				
				for(TakeoffItem item :to.getItems()){
					item.setTakeoff(null);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		} finally {
			if(to==null){
				to = Takeoff.makeTemplete();
				to.setCccc(airport);
			}
		}
		to.setPublishTime(new Date());
    	return new ResponseEntity(to,HttpStatus.OK);
    }
    @GetMapping(value="/takeoff/queryMessage")
    public ResponseEntity queryMessage(@RequestParam(value="start",required=false)Date start,@RequestParam(value="end",required=false)Date end){

		Map res = new HashMap();
    	List<Takeoff> list = new ArrayList<Takeoff>();
    	List<Takeoff> listByUser = new ArrayList<Takeoff>();
    	try {
    		list = takeoffService.getTakeoffList(start, end);
    		UserDTO userDto = userService.findById(SecurityUtils.getUserId());
    		

    		for(Takeoff to : list){
    			for(TakeoffItem item :to.getItems()){
    				item.setTakeoff(null);
    			}
    			if(null==userDto.getDept().getAirportCode()||userDto.getDept().getAirportCode().equalsIgnoreCase(to.getCccc())){
					listByUser.add(to);
    			}
    		}
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		res.put("totalElements", listByUser.size());
		res.put("content", listByUser);
    	return new ResponseEntity(res,HttpStatus.OK);
    }

    @PostMapping(value = "/takeoff/saveMessage")
    public ResponseEntity saveMessage(@RequestBody Takeoff takeoff){
    	try {
//    		takeoff.setPublishTime(new Date());
    		if(takeoff.getId()!=null)takeoff.setAmdIndex(takeoff.getAmdIndex()+1);
    		else{
    			UserDTO userDto = userService.findById(SecurityUtils.getUserId());
    			takeoff.setUsername(userDto.getDept().getName()+"/"+userDto.getUsername());
    		}
			takeoffService.save(takeoff);
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
	        return new ResponseEntity(false, HttpStatus.OK);
		}
        return new ResponseEntity(true, HttpStatus.OK);
    }
}
